19420471introtoitfandomcom-20200214-history
Methods
Introduction A method in Java is a group of statements executed. An example method is the System.out.println(), which when called, makes the system execute several statements to display a message. Parts of a method header Format: public static float validate(float value) Method modifier: * public static float validate(float value) * public - means the method is publicly available to code outside the class * static - means the method belongs to a class, not a specific object Return Type: * public static float '''validate(float value) * Return type - the data type of the value being returned by the method * If the method returns no data then the return type if void Method Name: * public static float '''validate(float value) * parameters - list of one or more variable declarations if the method is capable of receiving data (arguments) * parameter list may be empty if the method doesn't need data to function Identifying methods Example for getting input data while data fails validation display 'error message' get input data Generalise the code, and give it a name Call it whenever it is needed validate (value) while value <= 0 display 'error message' get value Once a method has been defined, it can be reused whenever it is needed Returning a value from a Method The return statement causes the method to end execution and it returns a value back to the statement that called the method Example: public static float validate(float value) //return type while(value <= 0) { System.out.print("Error in input. Must be > 0"); value = kb.nextFloat(); } return value; //This expression must be of the same data type as the return type } Example 2: public static int sum(int num1, int num2) { return num1 + num2; //This expression must be of the same data type as the return type } Calling a method A method executes only when it is called. The main method is automatically called when a program starts, but other methods are executed by method call statements. Example: exchangeRate = validate(exchangeRate); This can be interpreted as: Run the validate method, sending the current value of exchangeRate 'to the method, then assign the value returned from the method to the local variable exchangeRate. Calling a value-returning method Format: public static int sum(int num1, int num2){ int result; result = num1 + num2; return result; } Example lines of code: int value1 = 100, value2 = 450; int total = sum(value1, value2); System.out.println("sum = " + sum(value1, value2)); if (sum(value1, value2) > 500 System.out.println("You win!"); Passing Arguments to a Method Values that are sent into a method are called arguments. Example: System.out.println("'Hello"); exchangeRate = validate(exchangeRate); When you pass an argument to a method, be sure that the argument data type is compatible with the parameter data type. Java will automatically perform widening conversions, but narrowing will cause a compiler error. Sending a float argument into a double parameter is ok, but sending a double argument into a float argument is not. Example: double exchangeRate = 10.0; float newExchange = validate(exchangeRate); //This is incorrect